package com.qf.mrboot2.util;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.16</version>
</dependency>
*/
public class SmsUtil {
    public static String SendMsg(String tel) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", "LTAI5tRq22bWeL8ccKwPkhBW", "AMNWFppCzNR4qjuttZg7EyUVE9OXHx");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", tel);
        request.putQueryParameter("SignName", "阳阳商城");
        request.putQueryParameter("TemplateCode", "SMS_205409285");
        String code = UUID.randomUUID().toString().substring(0, 5);
        HashMap<String, String> map = new HashMap<>();
        map.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            //判断
            boolean success = response.getHttpResponse().isSuccess();
            if(success){
                return code;
            }else return null;
        } catch (ServerException e) {
            e.printStackTrace();
             return null;
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }

    }
}
